home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2426 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.7 KB  |  55 lines

  1. Path: news.th-darmstadt.de!news!enno
  2. From: enno@inferenzsysteme.informatik.th-darmstadt.de (Enno Sandner)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Overload of operator=
  5. Date: 17 Jan 1996 19:08:05 GMT
  6. Organization: Fachbereich Informatik, TH Darmstadt
  7. Distribution: world
  8. Message-ID: <ENNO.96Jan17200805@kitz.inferenzsysteme.informatik.th-darmstadt.de>
  9. References: <30FBCAA1.34A7@novell.com>
  10. NNTP-Posting-Host: kitz.intellektik.informatik.th-darmstadt.de
  11. In-reply-to: Greg Johnson's message of Tue, 16 Jan 1996 15:56:17 +0000
  12.  
  13. In article <30FBCAA1.34A7@novell.com> Greg Johnson <gregjo@novell.com> writes:
  14.  
  15.    I want to overload the assignment operator, for some debugging purposes.
  16.    But after I overload the operator, somehow, I need to perform the 
  17.    default assignment. How do I do that if I don't know the exact size
  18.    of the source or destination?
  19.    How do I avoid recursion and perform the default behavior?
  20.    For example:
  21.  
  22.    class BaseObj {
  23.    public:
  24.        long x;       
  25.        BaseObj& operator=(BaseObj&);
  26.        BaseObj(void) : x(0) {};
  27.    };
  28.  
  29.    BaseObj& BaseObj::operator=(BaseObj  & ptr)
  30.    {
  31.        *this = ptr; // this will recursively call operator=
  32.        return *this;
  33.    }
  34.  
  35.    void foo()
  36.    {
  37.        BaseObj bo1;
  38.        BaseObj bo2;
  39.  
  40.        bo1.x = 99;
  41.        bo2 = bo1;
  42.    }
  43.  
  44.    in operator=, the '*this = ptr' line causes a recursive call.
  45.    How can I avoid the recursion and perform the correct assignment?
  46.    I could do a memcpy, but I need to determine the size of the source
  47.    and destination (either one may be base pointers to a more complex 
  48.    class.)
  49.    Is there a way to determine the size of an object created with 'new'?
  50.  
  51. Just drop the definition of assignment operator in the non-debugging
  52. code. The default assignment-op does exactly what you are looking for.
  53.  
  54.         Enno
  55.